<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/5/19
 * Time: 13:37
 */

namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
use think\Request;

class Duty extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';

    public function work()
    {
        if (!Request::instance()->isGet())
        {
            $this->error('请求方式错误');
        }
        //->where('time',date('Y-m-d'))
        $list = Db::name('work')->select();
        if($list)
        {
            $this->success('',$list);
        }
        else
        {
            $this->error('暂无数据');
        }
    }

    //候车点名
    public function riding()
    {
        if (!Request::instance()->isGet())
        {
            $this->error('请求方式错误');
        }
        $info = $this->auth->getUserinfo();

        $params = input('get.');
        $where = [];
        if(!isset($params['circuit_id']) || $params['circuit_id']=='')
        {
            $this->error('缺少参数');
        }

        $where['a.circuit_id'] = $params['circuit_id'];

        $res = Db::name('sign_in')->where(['time'=>date('Y-m-d'),'circuit_id'=>$params['circuit_id'],'admin_id'=>$this->auth->id])->find();

        $status = 0;
        if($res)
        {
            $status = 1;
        }
        $class_room = Db::name('class_room')->field('id,title')->select();
        $class_arr = [];
        if($class_room)
        {
            foreach ($class_room as $k=>$v)
            {
                $class_arr[$v['id']] = $v['title'];
            }
        }


        $student = Db::name('automatic_student')->alias('a')
            ->join('fa_on_off_waiting b','a.RC_NUM=b.RC_NUM','left')
            //->where(['a.circuit_id'=>['in',$info['circuit_id']]])
            ->where($where)
            ->field('a.id,a.RC_NUM,a.class_room_id,a.point,a.name,a.circuit_id,a.state,b.circuit,b.RC_NUM as status')

            ->select();

        if($student)
        {
            foreach ($student as $key=>&$value)
            {
                $value['class_room'] = isset($class_arr[$value['class_room_id']])?$class_arr[$value['class_room_id']]:'';
                if($value['status']==NULL)
                {
                    $value['status'] = '未打卡';
                }
                else
                {
                    $value['status'] = '已打卡';
                }
            }
        }
        $this->success('',['data'=>$student,'status'=>$status]);

    }

    public function save_work()
    {
        if (!Request::instance()->isPost())
        {
            $this->error('请求方式错误');
        }

        $params = input('get.');
        if(!isset($params['id']) || $params['id']=='')
        {
            $this->error('缺少参数');
        }

        $res = Db::name('work')->where('id',$params['id'])->update(['status'=>1]);
        if($res)
        {
            $this->success('修改成功');
        }
        else
        {
            $this->error('修改失败');
        }

    }

    //学生乘车管理
    public function student()
    {
        if (!Request::instance()->isGet())
        {
            $this->error('请求方式错误');
        }
        $info = $this->auth->getUserinfo();

        $params = input('get.');
        $where = [];
        if(isset($params['circuit_id']) && $params['circuit_id']!='')
        {
            $where['b.circuit_id'] = $params['circuit_id'];
        }
        $circuit = Db::name('circuit')->select();
        $circuit_arr = [];
        foreach ($circuit as $k=>$v)
        {
            $circuit_arr[$v['id']] = $v;
        }
        //dump($class_arr);exit;

        $student = Db::name('student_information')->where('class_room_id',$info['class_room_id'])->select();

        $student_arr = [];
        if($student)
        {
            foreach ($student as $k=>$v)
            {
                $student_arr[$v['RC_NUM']] = $v;
            }
        }
        $leave = Db::name('leave')->field('student_id,createtime')->select();//->where('time',date('Y-m-d'))
        $leave_arr = [];
        if($leave)
        {
            foreach ($leave as $k=>$v)
            {
                $leave_arr[$v['student_id']] = $v;
            }
        }

        $student = Db::name('automatic_student')->alias('a')
            //->join('fa_on_off_car b','a.RC_NUM=b.RC_NUM','left')
            ->join('fa_student_information b','a.RC_NUM=b.RC_NUM')
            //->where(['a.class_room_id'=>$info['class_room_id']])
            ->where($where)
            //->field('a.student_id,a.RC_NUM,a.class_room_id,a.state,b.RC_TYPE,b.circuit,b.type,b.site,b.class_room,b.TIME')
            ->field('a.student_id,a.RC_NUM,a.class_room_id,a.state,b.name,b.student_number,b.circuit_id,b.point')
            ->select();
        if(!$student)
        {
            $this->error('暂无数据');
        }
        $on  = ['on'=>['data'=>[],'total'=>['on'=>0,'ont'=>0,'leave'=>0]],'off'=>['data'=>[],'total'=>['on'=>0,'ont'=>0,'leave'=>0]]];
        $off = ['on'=>['data'=>[],'total'=>['on'=>0,'ont'=>0,'leave'=>0]],'off'=>['data'=>[],'total'=>['on'=>0,'ont'=>0,'leave'=>0]]];
        ;
        if($student)
        {
            $on  = ['on'=>['data'=>$student,'total'=>['on'=>0,'ont'=>0,'leave'=>0]],'off'=>['data'=>$student,'total'=>['on'=>0,'ont'=>0,'leave'=>0]]];
            $off = ['on'=>['data'=>$student,'total'=>['on'=>0,'ont'=>0,'leave'=>0]],'off'=>['data'=>$student,'total'=>['on'=>0,'ont'=>0,'leave'=>0]]];
        }

        $on_off_car = Db::name('on_off_car')->select();//->where('DTIME',date('Y-m-d'))

        $off_car_arr = [];
        if($on_off_car)
        {
            foreach ($on_off_car as $k=>$v)
            {
                $off_car_arr[$v['RC_NUM'].'-'.$v['type'].'-'.$v['RC_TYPE']] = $v;
            }
        }


        foreach ($on as $k1=>$v1)
        {

            foreach ($v1['data'] as $k2=>&$v2)
            {
                $on[$k1]['data'][$k2]['TIME'] = '';
                $on[$k1]['data'][$k2]['circuit'] = isset($circuit_arr[$v2['circuit_id']])?$circuit_arr[$v2['circuit_id']]['title']:'';
                if($k1=='on')
                {
                    $isset = $v2['RC_NUM'].'-1-1';
                }
                else
                {
                    $isset = $v2['RC_NUM'].'-1-2';
                }
                $on[$k1]['data'][$k2]['status'] = 3;
                if(isset($leave_arr[$v2['student_id']]) )
                {
                    $on[$k1]['data'][$k2]['status'] = 2;
                    $on[$k1]['data'][$k2]['TIME'] = date('H:i',$leave_arr[$v2['student_id']]['createtime']);
                }
                if(isset($off_car_arr[$isset]))
                {
                    $on[$k1]['data'][$k2]['status'] =0;
                    $on[$k1]['data'][$k2]['TIME'] = date('H:i',$off_car_arr[$isset]['TIME']);
                    $on[$k1]['data'][$k2]['circuit'] = $off_car_arr[$isset]['circuit'];
                    $on[$k1]['data'][$k2]['point'] = $off_car_arr[$isset]['site'];
                }


                if($on[$k1]['data'][$k2]['status']==3)
                {
                    $on[$k1]['total']['ont']+=1;
                }
                else if($on[$k1]['data'][$k2]['status']==0)
                {
                    $on[$k1]['total']['on']+=1;
                }
                else
                {
                    $on[$k1]['total']['leave']+=1;
                }

            }
        }
        //dump($on);exit;
        array_multisort(array_column($on['on']['data'], 'status'),SORT_DESC,$on['on']['data']);
        array_multisort(array_column($on['off']['data'], 'status'),SORT_DESC,$on['off']['data']);

        foreach ($off as $k1=>$v1)
        {

            foreach ($v1['data'] as $k2=>&$v2)
            {
                $off[$k1]['data'][$k2]['TIME'] = '';
                $off[$k1]['data'][$k2]['circuit'] = isset($circuit_arr[$v2['circuit_id']])?$circuit_arr[$v2['circuit_id']]['title']:'';
                if($k1=='on')
                {
                    $isset = $v2['RC_NUM'].'-2-1';
                }
                else
                {
                    $isset = $v2['RC_NUM'].'-2-2';
                }
                $off[$k1]['data'][$k2]['status'] = 3;

                if(isset($leave_arr[$v2['student_id']]))
                {

                    $off[$k1]['data'][$k2]['status'] = 2;
                    $off[$k1]['data'][$k2]['TIME'] = date('H:i',$leave_arr[$v2['student_id']]['createtime']);
                }
                if(isset($off_car_arr[$isset]))
                {
                    $off[$k1]['data'][$k2]['status'] =0;
                    $off[$k1]['data'][$k2]['TIME'] = date('H:i',$off_car_arr[$isset]['TIME']);
                    $off[$k1]['data'][$k2]['circuit'] = $off_car_arr[$isset]['circuit'];
                    $off[$k1]['data'][$k2]['point'] = $off_car_arr[$isset]['site'];
                }

                if($off[$k1]['data'][$k2]['status']==3)
                {
                    $off[$k1]['total']['ont']+=1;
                }
                else if($on[$k1]['data'][$k2]['status']==0)
                {
                    $off[$k1]['total']['on']+=1;
                }
                else
                {
                    $off[$k1]['total']['leave']+=1;
                }

            }
        }
        array_multisort(array_column($off['on']['data'], 'status'),SORT_DESC,$off['on']['data']);
        array_multisort(array_column($off['off']['data'], 'status'),SORT_DESC,$off['off']['data']);

        $this->success('',['on'=>$on,'off'=>$off]);

    }



}